package ylsb.controller;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import ylsb.model.YltpJieguo;
import ylsb.model.YltpRenyuanfenpei;
import ylsb.model.YltpXiangmu;
import ylsb.service.*;
import ylsb.support.CreateXLS;
import ylsb.support.JsonResult;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.*;

@RestController
@RequestMapping("renyuanfenpei")
public class YltprenyuanfenpeiController {

    @Value("${userfilepath}")
    private String userfilepath;
    @Resource
    YltprenyuanfenpeiService ddService;
    @Resource
    HaocaiyinjinService ddHaocaiyinjinService;
    @Resource
    HaocaijieguoService ddHaocaijieguoService;
    @Resource
    TiwaizhenduanService ddTiwaizhenduanService;
    @Resource
    TiwaijieguoService ddTiwaijieguoService;
    @Resource
    YltpXiangmuService ddYltpXiangmuService;
    @Resource
    YltpbaofeiService ddYltpbaofeiService;
    @Resource
    YltpyinjinService ddYltpyinjinService;
    @Resource
    YltpjieguoService ddYltpjieguoService;
    @Resource
    DailiangchuangxinService ddDailiangchuangxinService;


    public Logger log = LoggerFactory.getLogger(YltprenyuanfenpeiController.class);

    @RequestMapping("checkIsAllSubmited")
    public JsonResult<?> checkIsAllSubmited(
            @RequestParam("xiangmuid") String ppxiangmuId,
            @RequestParam("userid") String ppuserId){

        List<YltpRenyuanfenpei> mmUserList=ddService.findUserByXiangmuId(ppxiangmuId);
        Integer mmSubmitedCount=ddService.getSubmitedCount(ppxiangmuId);
        if(mmUserList.size()==mmSubmitedCount){
            return  JsonResult.getSuccessResult("1");
        }else {
            return  JsonResult.getSuccessResult("0");
        }
    }

    @RequestMapping("findRenyuanFenpeiByXiangmuId")
    public JsonResult<?> findRenyuanFenpeiByXiangmuId(@RequestParam("xiangmuid") String ppxianmguId){
        try {
            return JsonResult.getSuccessResult(ddService.findRenyuanFenpeiByXiangmuId(ppxianmguId));

        }catch (Exception e) {
            e.printStackTrace();
            log.error("renyuanfenpei/findRenyuanFenpeiByXiangmuId:error",e);
            return JsonResult.getErrorResult("renyuanfenpei/findRenyuanFenpeiByXiangmuId:error " + e.getMessage());
        }
    }
    @RequestMapping("bohui")
    public JsonResult<?> bohui(@RequestParam("id") String ppId){
        try {

            YltpRenyuanfenpei mmRenyuanfenpei=new YltpRenyuanfenpei();
            mmRenyuanfenpei.setId(ppId);
            mmRenyuanfenpei.setIssubmit(0);
            return ddService.updateByPrimaryKeySelective(mmRenyuanfenpei)==true? JsonResult.getSuccessResult("驳回成功")
                    : JsonResult.getErrorResult("驳回失败");

        }catch (Exception e) {
            e.printStackTrace();
            log.error("renyuanfenpei/bohui:error",e);
            return JsonResult.getErrorResult("renyuanfenpei/bohui:error " + e.getMessage());
        }
    }

    @RequestMapping("add")
    public JsonResult<?> add(
            @RequestParam("xiangmuid") String ppxiangmuId,
            @RequestParam("userid") String ppuserId){
        try{
            if(ddService.findbyUserIDandXiangmuId(ppuserId,ppxiangmuId).size()>0){
                return JsonResult.getErrorResult("加入失败！该人员已加入当前活动项目中。");
            }
            if(ddService.findUserNumberByXiangmuId(ppxiangmuId).size()>=ddYltpXiangmuService.findOne(ppxiangmuId).getZhuangjiashu()){
                return JsonResult.getErrorResult("加入失败！该项目人员已满。");
            }

            YltpRenyuanfenpei mmRenyuanfenpei=new YltpRenyuanfenpei();
            mmRenyuanfenpei.setId(UUID.randomUUID().toString());
            mmRenyuanfenpei.setXiangmuid(ppxiangmuId);
            mmRenyuanfenpei.setUserid(ppuserId);
            mmRenyuanfenpei.setIssubmit(0);
            return ddService.add(mmRenyuanfenpei)==true? JsonResult.getSuccessResult("新增成功")
                    : JsonResult.getErrorResult("新增失败");
        }catch (Exception e) {
            e.printStackTrace();
            log.error("renyuanfenpei/add:error",e);
            return JsonResult.getErrorResult("renyuanfenpei/add:error " + e.getMessage());
        }

    }

    @RequestMapping("submit")
    public JsonResult<?> submit(
            @RequestParam("xiangmuid") String ppxiangmuId,
            @RequestParam("userid") String ppuserId){
        try{
            List<YltpRenyuanfenpei>  list=ddService.findbyUserIDandXiangmuId(ppuserId,ppxiangmuId);
            if(list.size()<1){
                return JsonResult.getErrorResult("提交失败，人员-项目分配信息不存在");
            }
            int mmTotal=0;
            int mmVoted=0;
            YltpXiangmu mmYltpXiangmu=ddYltpXiangmuService.findOne(ppxiangmuId);
            if("1".equals(mmYltpXiangmu.getXiangmuleixing())){
                mmTotal=ddYltpyinjinService.getVoteCount(ppxiangmuId);
                mmVoted=ddYltpjieguoService.getYinjinResultCount(ppxiangmuId,ppuserId,"");
            }else{
                return JsonResult.getErrorResult("提交失败，没有此类型。");
            }
            /*  if("2".equals(mmYltpXiangmu.getXiangmuleixing())){
                mmTotal=ddYltpbaofeiService.getVoteCount(ppxiangmuId);
                mmVoted=ddYltpjieguoService.getBaofeiResultCount(ppxiangmuId,ppuserId,"");
            }
            if("5".equals(mmYltpXiangmu.getXiangmuleixing())){
                mmTotal=ddHaocaiyinjinService.getVoteCount(ppxiangmuId);
                mmVoted=ddHaocaijieguoService.getHaocaiduibiaoResultCount(ppxiangmuId,ppuserId,"");
            }
            if("6".equals(mmYltpXiangmu.getXiangmuleixing())){
                mmTotal=ddTiwaizhenduanService.getVoteCount(ppxiangmuId);
                mmVoted=ddTiwaijieguoService.getTiWaiResultCount(ppxiangmuId,ppuserId,"");
            }*/
           /* if("8".equals(mmYltpXiangmu.getXiangmuleixing())){
                mmTotal=ddDailiangchuangxinService.getChuangxinCount(ppxiangmuId);
                mmVoted=ddYltpjieguoService.getChuangxinchanpinResultCount(ppxiangmuId,ppuserId,"1");
            }
            if("9".equals(mmYltpXiangmu.getXiangmuleixing())){
                mmTotal=ddDailiangchuangxinService.getChuangxinCount(ppxiangmuId);
                mmVoted=ddYltpjieguoService.getChuangxinchanpinResultCount(ppxiangmuId,ppuserId,"1");
            }*/

            if(mmTotal!=mmVoted){
               /* return JsonResult.getErrorResult("提交失败，请全部选择同意。");*/
                return JsonResult.getErrorResult("提交失败，没有全部表决建议。");
            }

            YltpRenyuanfenpei mmRenyuanfenpei=new YltpRenyuanfenpei();
            mmRenyuanfenpei.setId(list.get(0).getId());
            mmRenyuanfenpei.setUserid(ppuserId);
            mmRenyuanfenpei.setXiangmuid(ppxiangmuId);
            mmRenyuanfenpei.setIssubmit(1);
            return ddService.update(mmRenyuanfenpei)==true? JsonResult.getSuccessResult("提交成功")
                    : JsonResult.getErrorResult("提交失败");
        }catch (Exception e) {
            e.printStackTrace();
            log.error("renyuanfenpei/submit:error",e);
            return JsonResult.getErrorResult("renyuanfenpei/submit:error " + e.getMessage());
        }

    }

    @RequestMapping("checkIsSubmited")
    public JsonResult<?> checkIsSubmited(
            @RequestParam("xiangmuid") String ppxiangmuId,
            @RequestParam("userid") String ppuserId){
        List<YltpRenyuanfenpei>  list=ddService.findbyUserIDandXiangmuId(ppuserId,ppxiangmuId);
        if(list.size()<1){
            return JsonResult.getErrorResult("错误，人员-项目分配信息不存在");
        }
        if("1".equals(list.get(0).getIssubmit().toString())){
            return  JsonResult.getSuccessResult("1");
        }else {
            return  JsonResult.getSuccessResult("0");
        }
    }

    @RequestMapping("delete")
    public JsonResult<?> delete(@RequestParam("id") String ppId){
        try {

            return ddService.delete(ppId)==true? JsonResult.getSuccessResult("删除成功")
                    : JsonResult.getErrorResult("删除失败");

        }catch (Exception e) {
            e.printStackTrace();
            log.error("renyuanfenpei/delete:error",e);
            return JsonResult.getErrorResult("renyuanfenpei/delete:error " + e.getMessage());
        }
    }

    @RequestMapping("exportRenyuan")
    public String exportRenyuan(HttpServletRequest request, HttpServletResponse response,
                                @RequestParam("xiangmuId") String ppxiangmuid

                                ) {
        List<List<String>> mmResultList = new ArrayList<List<String>>();
        List<String> mmTitleList = new ArrayList<String>();

        mmTitleList.add("序号");
        mmTitleList.add("姓名");
        mmTitleList.add("部职别");
        mmTitleList.add("账号");
        mmTitleList.add("密码");
        mmResultList.add(mmTitleList);

        List<Map<String, Object>> mmXiangmuList = ddService.findRenyuanFenpeiByXiangmuId(ppxiangmuid);
        int mmXiangmushu = mmXiangmuList.size();
        if(mmXiangmushu > 0) {
            for (int i = 0; i < mmXiangmushu; i++) {
                Map<String, Object> mmXiangmuMap=mmXiangmuList.get(i);
                String mmXuhao = String.valueOf(i + 1);
                String mmUsername=mmXiangmuMap.get("username")==null ? "" :mmXiangmuMap.get("username").toString();
                String mmBuzhibie=mmXiangmuMap.get("buzhibie")==null ? "" :mmXiangmuMap.get("buzhibie").toString();
                String mmAccount=mmXiangmuMap.get("account")==null ? "" :mmXiangmuMap.get("account").toString();
                String mmPassword=mmXiangmuMap.get("password")==null ? "" :mmXiangmuMap.get("password").toString();

                List<String> mmTempList = new ArrayList<String>();
                mmTempList.add(mmXuhao);
                mmTempList.add(mmUsername);
                mmTempList.add(mmBuzhibie);
                mmTempList.add(mmAccount);
                mmTempList.add(mmPassword);

                mmResultList.add(mmTempList);

            }
        }

        try {
            //取出文档存放的根目录，并去除文件最后的斜杠
            if (userfilepath.endsWith("/")) {
                userfilepath = userfilepath.substring(0, userfilepath.length()-1);
            }

            SimpleDateFormat mmDateFormat = new SimpleDateFormat("yyyyMMdd");
            String mmSavePath = userfilepath +  "/temp/"+mmDateFormat.format(new Date()) + "/";

            CreateXLS.createExcel(mmResultList, mmSavePath,  "renyuanmingdan_"+ mmDateFormat.format(new Date())+".xls",
                    "yiliaoshebie", request, response);
            return "/ylsbtp/yiliaoshebie.xls";

        }
        catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return e.getMessage();
        }
    }
}
